package com.hmall.search.controller;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.domain.PageDTO;
import com.hmall.search.domain.dto.ItemDTO;
import com.hmall.search.domain.po.ItemDoc;
import com.hmall.search.domain.query.ItemPageQuery;
import com.hmall.search.service.IItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 */
@Api(tags = "搜索相关接口")
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
@Slf4j
public class SearchController {

    private final IItemService itemService;

    @Autowired
    private ElasticsearchClient esClient;

//    /*
//    es搜索
//     */
//    @ApiOperation("搜索商品")
//    @GetMapping("/list")
//    public PageDTO<ItemDTO> search(ItemPageQuery query) {
//        // 分页查询
//        Page<Item> result = itemService.lambdaQuery()
//                .like(StrUtil.isNotBlank(query.getKey()), Item::getName, query.getKey())
//                .eq(StrUtil.isNotBlank(query.getBrand()), Item::getBrand, query.getBrand())
//                .eq(StrUtil.isNotBlank(query.getCategory()), Item::getCategory, query.getCategory())
//                .eq(Item::getStatus, 1)
//                .between(query.getMaxPrice() != null, Item::getPrice, query.getMinPrice(), query.getMaxPrice())
//                .page(query.toMpPage("update_time", false));
//        // 封装并返回
//        return PageDTO.of(result, ItemDTO.class);
//    }


    @ApiOperation("es搜索商品")
    @GetMapping("/list")
    public PageDTO<ItemDTO> search(ItemPageQuery query) throws Exception {
        SearchResponse<ItemDoc> searchResponse = esClient.search(s -> s
                .index("items")
                .query(q -> q
                        .match(t -> t
                                .field("name").query(query.getKey()))), ItemDoc.class);

        // 解析结果并转换为 PageDTO
        return handleResponse(searchResponse, query);
    }

    private PageDTO<ItemDTO> handleResponse(SearchResponse<ItemDoc> searchResponse, ItemPageQuery query) {
        // 获取总条数
        long total = searchResponse.hits().total().value();
        log.info("查询到{}条数据", total);

        // 解析结果
        List<Hit<ItemDoc>> hits = searchResponse.hits().hits();
        List<ItemDTO> itemDTOList = hits.stream()
                .map(hit -> {
                    ItemDoc source = hit.source();
                    log.info("查询到数据：{}", source);
                    return new ItemDTO(); // 建议根据 ItemDoc 构造 ItemDTO
                })
                .collect(Collectors.toList());

        // 构造分页结果
        Page<ItemDTO> page = new Page<>(query.getPageNo(), query.getPageSize());
        page.setTotal(total);
        page.setRecords(itemDTOList);

        return PageDTO.of(page, ItemDTO.class);
    }
}
